﻿function _createPopup(e) {
    var $popupContainer = $(document.createElement('article'));
    var $popupShow = $(document.createElement('section'));
    var $popupContent = $(document.createElement('article'));
    var $popupClose = $(document.createElement('section'));
    var $popupCloseButton = $(document.createElement('i'));
    var $popupLoading = $('<article class="loading"><ul><li>•</li><li>•</li><li>•</li><li>•</li><li>•</li></ul></article>');

    $popupContainer.addClass('popup-container');
    $popupClose.addClass('popup-close');
    $popupShow.addClass('popup-show');
    $popupContent.addClass('popup-content');
    $popupCloseButton.addClass('popup-close-button');

    $popupContainer.append($popupClose);
    $popupContainer.append($popupShow);
    $popupShow.append($popupContent);
    $popupShow.append($popupCloseButton);
    $popupShow.append($popupLoading);

    $('body').append($popupContainer);

    $popupClose.click(function () {
        $popupContainer.fadeOut(100);
        setTimeout(function () {
            $popupContainer.remove();
        }, 100);
    });
    $popupCloseButton.click(function () {
        $popupContainer.fadeOut(100);
        setTimeout(function () {
            $popupContainer.remove();
        }, 100);
    });

    e.preventDefault();

    _ajax(
        e.target,
        null,
        'POST',
        'json',
        function (data) {
            $popupLoading.remove();
            if (data.trangThai == 'OK') {
                $popupContent.html(data.ketQua);
                setTimeout(function () {
                    $popupContent.css('opacity', '1');
                    $popupShow.css({
                        left: 'calc( 50% - ' + $popupContent.width() / 2 + 'px)',
                        top: 'calc( 30vh - ' + $popupContent.height() / 2 + 'px)',
                        //top: (window.innerHeight - $popupContent.height()) / 4 + 'px',
                        //top: 'calc( 50% - ' + $popupContent.height() / 2 + 'px)',
                        width: $popupContent.width() + 'px',
                        height: $popupContent.height() + 'px'
                    });
                }, 100);
            }
            else {
                $popupContainer.fadeOut(100);
                setTimeout(function () {
                    $popupContainer.remove();
                }, 100);
            }
        },
        function () {
            $popupContainer.fadeOut(100);
            setTimeout(function () {
                $popupContainer.remove();
            }, 100);
        },
        ''
    );
}

function _ajax(url, data, type, dataType, successFunction, errorFunction, loadingId, functionName) {
    $('body').addClass('load');
    $('#' + loadingId).addClass('ajax-loading');
    $.ajax({
        url: url,
        data: data,
        type: type,
        dataType: dataType,
        success: function (data) {
            successFunction(data);
        },
        error: function (jqXHR) {
            errorFunction(functionName + ' -- ' + jqXHR.status);
        },
        complete: function () {
            $('body').removeClass('load');
            $('#' + loadingId).removeClass('ajax-loading');
        }
    })
}

function _ajaxForm(url, data, type, dataType, mimeType, contentType, cache, processData, successFunction, errorFunction, loadingId, functionName) {
    $('body').addClass('load');
    var e = window.event;
    e.preventDefault();
    e = e.srcElement;
    $('#' + loadingId).addClass('ajax-loading');
    $.ajax({
        url: url,
        data: data,
        type: type,
        dataType: dataType,
        mimeType: mimeType,
        contentType: contentType,
        cache: cache,
        processData: processData,
        success: function (data) {
            successFunction(data);
        },
        error: function (jqXHR) {
            errorFunction(functionName + ' -- ' + jqXHR.status);
        },
        complete: function () {
            $('body').removeClass('load');
            $('#' + loadingId).removeClass('ajax-loading');
        }
    })
}

function Report_Ajax(text, stt) {
    $.ajax({
        url: '/Shared/Report',
        data: { text: text, stt: stt },
        complete: function (data) {
            $('body').append(data.responseText);
            if (stt != 'b') {
                timeOut = setTimeout("Out()", 3000);
            }
            slideAtStart();
        }
    })
}